﻿using LiJu.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LiJu
{
    public partial class ParmeterEdit : Form
    {
        /// <summary>
        /// Step信息类
        /// </summary>
        public StepModel stepModel { get; set; }

        public bool IsSave { get; set; }

        public ParmeterEdit()
        {
            InitializeComponent();
        }

        private void ParmeterEdit_Load(object sender, EventArgs e)
        {
            LblStep.Text = stepModel.Step;//步骤
            LblStepType.Text = stepModel.StepType;//步骤类型
            LblTighteningType.Text = stepModel.TighteningType;//拧紧类型

            TbxLap.Text = stepModel.Lap;//圈数
            TbxLap.Enabled = false;

            TbxSpeed.Text = stepModel.Speed;//速度
            TbxSpeed.Enabled = !string.IsNullOrWhiteSpace(stepModel.Speed);

            TbxMiniTorqueLength.Text = stepModel.MiniTorqueLength;//最小力矩
            TbxMiniTorqueLength.Enabled = !string.IsNullOrWhiteSpace(stepModel.MiniTorqueLength);

            TbxMaxTorqueLength.Text = stepModel.MaxTorqueLength;//最大力矩
            TbxMaxTorqueLength.Enabled = !string.IsNullOrWhiteSpace(stepModel.MaxTorqueLength);

            TbxSafeTorqueLength.Text = stepModel.SafeTorqueLength;//安全力矩
            TbxSafeTorqueLength.Enabled = !string.IsNullOrWhiteSpace(stepModel.SafeTorqueLength);

            TbxAnglethreshold.Text = stepModel.Anglethreshold;//角度阈值
            TbxAnglethreshold.Enabled = !string.IsNullOrWhiteSpace(stepModel.Anglethreshold);

            TbxTargetTorqueLength.Text = stepModel.TargetTorqueLength;//目标力矩
            TbxTargetTorqueLength.Enabled = !string.IsNullOrWhiteSpace(stepModel.TargetTorqueLength);

            TbxMiniAngle.Text = stepModel.MiniAngle;//最小角度
            TbxMiniAngle.Enabled = !string.IsNullOrWhiteSpace(stepModel.MiniAngle);


            TbxMaxAngle.Text = stepModel.MaxAngle;//最大角度
            TbxMaxAngle.Enabled = !string.IsNullOrWhiteSpace(stepModel.MaxAngle);

            TbxSafeAngle.Text = stepModel.SafeAngle;//安全角度
            TbxSafeAngle.Enabled = !string.IsNullOrWhiteSpace(stepModel.SafeAngle);

            TbxTargetAngle.Text = stepModel.TargetAngle;//目标角度
            TbxTargetAngle.Enabled = !string.IsNullOrWhiteSpace(stepModel.TargetAngle);
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            stepModel.MiniTorqueLength = TbxMiniTorqueLength.Text;//最小力矩
            stepModel.MaxTorqueLength = TbxMaxTorqueLength.Text;//最大力矩
            stepModel.SafeTorqueLength = TbxSafeTorqueLength.Text;//安全力矩
            stepModel.Anglethreshold = TbxAnglethreshold.Text;//角度阈值
            stepModel.TargetTorqueLength = TbxTargetTorqueLength.Text;//目标力矩
            stepModel.MiniAngle = TbxMiniAngle.Text;//最小角度
            stepModel.MaxAngle = TbxMaxAngle.Text;//最大角度
            stepModel.SafeAngle = TbxSafeAngle.Text;//安全角度
            stepModel.TargetAngle = TbxTargetAngle.Text;//目标角度
            stepModel.Lap = TbxLap.Text;//圈数
            stepModel.Speed = TbxSpeed.Text;//速度

            bool Iserr = true;

            

            if (TbxMiniTorqueLength.Enabled && Convert.ToDouble(stepModel.MiniTorqueLength) < 0.01)
            {
                MessageBox.Show("【最小力矩】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxMiniTorqueLength.Focus();
                return;
            }
            if (TbxMaxTorqueLength.Enabled && Convert.ToDouble(stepModel.MaxTorqueLength) < 0.01)
            {
                MessageBox.Show("【最大力矩】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxMaxTorqueLength.Focus();
                return;
            }
            if (TbxSafeTorqueLength.Enabled && Convert.ToDouble(stepModel.SafeTorqueLength) < 0.01)
            {
                MessageBox.Show("【安全力矩】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxSafeTorqueLength.Focus();
                return;
            }
            if (TbxAnglethreshold.Enabled && Convert.ToDouble(stepModel.Anglethreshold) < 0.01)
            {
                MessageBox.Show("【角度阈值】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxAnglethreshold.Focus();
                return;
            }
            if (TbxTargetTorqueLength.Enabled && Convert.ToDouble(stepModel.TargetTorqueLength) < 0.01)
            {
                MessageBox.Show("【目标力矩】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxTargetTorqueLength.Focus();
                return;
            }
            if (TbxMiniAngle.Enabled && Convert.ToDouble(stepModel.MiniAngle) < 0.01)
            {
                MessageBox.Show("【最小角度】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxMiniAngle.Focus();
                return;
            }
            if (TbxMaxAngle.Enabled && Convert.ToDouble(stepModel.MaxAngle) < 0.01)
            {
                MessageBox.Show("【最大角度】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxMaxAngle.Focus();
                return;
            }
            if (TbxSafeAngle.Enabled && Convert.ToDouble(stepModel.SafeAngle) < 0.01)
            {
                MessageBox.Show("【安全角度】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxSafeAngle.Focus();
                return;
            }
            if (TbxTargetAngle.Enabled && Convert.ToDouble(stepModel.TargetAngle) < 0.01)
            {
                MessageBox.Show("【目标角度】填写值异常，不能小于【0.01】", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                TbxTargetAngle.Focus();
                return;
            }






            //最小力矩不要超过目标力矩
            if (TbxMiniTorqueLength.Enabled && TbxTargetTorqueLength.Enabled && Convert.ToDouble(stepModel.MiniTorqueLength) >= Convert.ToDouble(stepModel.TargetTorqueLength))
            {
                DialogResult dr = MessageBox.Show("最小力矩不要超过目标力矩，是否忽略继续保存？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.No)
                {
                    //设置焦点
                    TbxMiniTorqueLength.Focus();
                    Iserr = false;
                    TbxMiniTorqueLength.BackColor = Color.Red;
                }
            }

            //最小角度不要超过目标角度
            if (TbxMiniAngle.Enabled && TbxTargetAngle.Enabled && Convert.ToDouble(stepModel.MiniAngle) >= Convert.ToDouble(stepModel.TargetAngle))
            {
                DialogResult dr = MessageBox.Show("最小角度不要超过目标角度，是否忽略继续保存？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dr == DialogResult.No)
                {
                    //设置焦点
                    TbxMiniAngle.Focus();
                    Iserr = false;
                    TbxMiniAngle.BackColor = Color.Red;
                }
            }

            if (Iserr)
            {
                IsSave = true;

                MessageBox.Show("参数设置成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }

        }

        private void ParmeterEdit_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (!IsSave)
            {
                stepModel = null;
            }
        }

        private void TbxLap_Leave(object sender, EventArgs e)
        {

        }

        private void TbxSpeed_Leave(object sender, EventArgs e)
        {
            //速度 只能输入数字
            string pattern = @"^\d+$";

            var result = TextBoxIsMatchData(pattern, TbxSpeed, "速度不能填写字母或者符号且不能为空，范围值为【1-494】", stepModel.Speed);
            if (result && Convert.ToInt32(TbxSpeed.Text) > 494)
            {
                MessageBox.Show("速度填写值异常，范围值为【1-494】", "异常提示！");

                // 设置焦点
                TbxSpeed.Text = stepModel.Speed;
                TbxSpeed.Focus();
                return;
            }
        }

        /// <summary>
        /// 校验文本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TbxMaxAndMinLength_Leave(object sender, EventArgs e)
        {
            //1至2位小数 ，最大整数位4位
            string pattern = @"^\d{1,4}(?:\.\d{1,2})?$";

            TextBox textBox = (TextBox)sender;
            string parName = "";
            string oldValue = "";

            switch (textBox.Name)
            {
                case "TbxMiniTorqueLength":
                    parName = "最小力矩";
                    oldValue = stepModel.MiniTorqueLength;
                    break;
                case "TbxMaxTorqueLength":
                    parName = "最大力矩";
                    oldValue = stepModel.MaxTorqueLength;
                    break;
                case "TbxSafeTorqueLength":
                    parName = "安全力矩";
                    oldValue = stepModel.SafeTorqueLength;
                    break;
                case "TbxAnglethreshold":
                    parName = "角度阈值";
                    oldValue = stepModel.Anglethreshold;
                    break;
                case "TbxTargetTorqueLength":
                    parName = "目标力矩";
                    oldValue = stepModel.TargetTorqueLength;
                    break;
                case "TbxMiniAngle":
                    parName = "最小角度";
                    oldValue = stepModel.MiniAngle;
                    break;
                case "TbxMaxAngle":
                    parName = "最大角度";
                    oldValue = stepModel.MaxAngle;
                    break;
                case "TbxSafeAngle":
                    parName = "安全角度";
                    oldValue = stepModel.SafeAngle;
                    break;
                case "TbxTargetAngle":
                    parName = "目标角度";
                    oldValue = stepModel.TargetAngle;
                    break;
            }


            var result = TextBoxIsMatchData(pattern, textBox, $"{parName}不能填入字母或者符号且不能为空，范围值为【0.01-9999】", oldValue);
            if (result)
            {
                double value = Convert.ToDouble(textBox.Text);
                if (value < 0.01 || value > 9999)
                {
                    MessageBox.Show($"{parName}填写值异常，范围值为【0.01-9999】", "异常提示！");

                    // 设置焦点
                    textBox.Text = oldValue;
                    textBox.Focus();
                    return;
                }

                //目标角度时，计算圈数，圈数=目标角度/360
                if (textBox.Name == "TbxTargetAngle")
                {
                    double lap = Convert.ToDouble(textBox.Text) / 360;
                    TbxLap.Text = Math.Round(lap, 3).ToString();// lap.ToString();
                }

                //最小力矩，并且为异常时
                if (textBox.Name == "TbxMiniTorqueLength" && textBox.BackColor == Color.Red)
                {
                    if (value < Convert.ToDouble(TbxTargetTorqueLength.Text))
                    {
                        textBox.BackColor = Color.White;
                    }
                }

                //目标力矩>最小力矩 恢复正常
                if (textBox.Name == "TbxTargetTorqueLength" && TbxMiniTorqueLength.BackColor == Color.Red)
                {
                    if (value > Convert.ToDouble(TbxMiniTorqueLength.Text))
                    {
                        TbxMiniTorqueLength.BackColor = Color.White;
                    }
                }


                //最小角度，并且为异常时
                if (textBox.Name == "TbxMiniAngle" && textBox.BackColor == Color.Red)
                {
                    if (value < Convert.ToDouble(TbxTargetAngle.Text))
                    {
                        textBox.BackColor = Color.White;
                    }
                }

                //目标角度>最小角度 恢复正常
                if (textBox.Name == "TbxTargetAngle" && TbxMiniAngle.BackColor == Color.Red)
                {
                    if (value > Convert.ToDouble(TbxMiniAngle.Text))
                    {
                        TbxMiniAngle.BackColor = Color.White;
                    }
                }
            }
        }



        /// <summary>
        /// 校验输入的内容是否符合规则
        /// </summary>
        /// <param name="pattern">正则表达式</param>
        /// <param name="textBox">文本框</param>
        /// <param name="errMsg">消息提示</param>
        public bool TextBoxIsMatchData(string pattern, TextBox textBox, string errMsg, string oldValue = "")
        {

            //获取单元格的输入值
            string inputValue = textBox.Text;

            // 创建Regex对象并进行匹配测试
            Regex regex = new Regex(pattern);
            if (!regex.IsMatch(inputValue))
            {


                MessageBox.Show(errMsg, "异常提示！");

                // 设置焦点
                textBox.Text = oldValue;
                textBox.Focus();

                //目标角度时,赋原值
                if (textBox.Name == "TbxTargetAngle")
                {
                    TbxLap.Text = stepModel.Lap;
                }

                return false;

            }
            else
            {
                return true;
            }
        }
    }
}
